using net_work.Config;
using net_work.Data.DTO.Cou;
using net_work.Data.Entity.Cou;
using net_work.IRepository.Cou;

namespace net_work.Repository.Cou;

public class EnrollmentRepository(ApplicationDbContext dbContext)
    : Repository<Enrollment>(dbContext), IEnrollmentRepository
{
    public async Task<long> InsertAsync(EnrollmentAddRequestBackend enrollmentAddRequestBackend)
    {
        var newEnrollment = new Enrollment
        {
            Remark = enrollmentAddRequestBackend.Remark,
            StudentId = enrollmentAddRequestBackend.StudentId,
            TeachId = enrollmentAddRequestBackend.TeachId
        };
        await dbContext.Enrollments.AddAsync(newEnrollment);
        await dbContext.SaveChangesAsync();
        return newEnrollment.Id;
    }

    public async Task<bool> UpdateAsync(EnrollmentUpdateRequestBackend enrollmentUpdateRequestBackend)
    {
        var enrollment = await dbContext.Enrollments.FindAsync(enrollmentUpdateRequestBackend.Id);

        if (enrollment == null)
        {
            return false;
        }

        enrollment.Remark = enrollmentUpdateRequestBackend.Remark;
        enrollment.TeachId = enrollmentUpdateRequestBackend.TeachId;
        enrollment.StudentId = enrollmentUpdateRequestBackend.StudentId;

        await dbContext.SaveChangesAsync();
        return true;
    }
}